package com.bdth.mixapi.api.controller.traffic;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdth.mixapi.api.entity.Provincedistribution;
import com.bdth.mixapi.api.service.ProvincedistributionService;
import com.bdth.mixapi.api.vo.CommonList;
import com.bdth.mixapi.api.vo.CommonPage;
import com.bdth.mixapi.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2019-04-15
 */
@Api("按省份车辆统计")
@RestController
@RequestMapping("/traffic")
public class ProvincedistributionController {
    @Autowired
    ProvincedistributionService provincedistributionService;

    @ApiOperation(value="按省分在线率列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "provinceName", dataType = "String", required = false, value = "省份", defaultValue = ""),
    })
    @RequestMapping(value = "provincedistribution/list", method = RequestMethod.GET)
    public CommonList<Provincedistribution> list(@RequestParam(value = "provinceName",required=false) String provinceName){
        //查询有数据的最后一天
        QueryWrapper<Provincedistribution> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<Provincedistribution> list = provincedistributionService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<Provincedistribution> queryWrapper = new QueryWrapper<>();
            if (provinceName != null) {
                queryWrapper.like("provinceName", provinceName);
            }
            //2天前的统计数据
            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            queryWrapper.orderByDesc("OnlineRate");
            List<Provincedistribution> provincedistributionList = provincedistributionService.list(queryWrapper);
            return CommonList.success(provincedistributionList);
        }else{
            return CommonList.fail("没有数据");
        }
    }

    @ApiOperation(value="按省分在线率列表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "provinceName", dataType = "String", required = false, value = "省份", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "now", dataType = "int", required = false, value = "当前页码", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "rows", dataType = "int", required = false, value = "每页条数", defaultValue = "20")
    })
    @RequestMapping(value = "provincedistribution/page", method = RequestMethod.GET)
    public CommonPage<Provincedistribution> page(@RequestParam(value = "provinceName",required=false) String provinceName, @RequestParam Integer now, @RequestParam Integer rows){
        //查询有数据的最后一天
        QueryWrapper<Provincedistribution> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<Provincedistribution> list = provincedistributionService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<Provincedistribution> queryWrapper = new QueryWrapper<>();
            if (provinceName != null) {
                queryWrapper.like("provinceName", provinceName);
            }
            //2天前的统计数据
            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            Page<Provincedistribution> page = new Page<>(now == null ? 1 : now, rows == null ? 20 : rows);
            provincedistributionService.page(page, queryWrapper);
            CommonPage<Provincedistribution> result = new CommonPage<Provincedistribution>(page.getRecords(), page.getTotal());
            return result;
        }else{
            return new CommonPage<>(new ArrayList<>(),0);
        }
    }

    @ApiOperation(value="按省分车辆在线率TOP10")
    @RequestMapping(value = "provincedistribution/rateTop10", method = RequestMethod.GET)
    public CommonList<Provincedistribution> rateTop10(){
        //查询有数据的最后一天
        QueryWrapper<Provincedistribution> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<Provincedistribution> list = provincedistributionService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<Provincedistribution> queryWrapper = new QueryWrapper<>();
            //2天前的统计数据
            queryWrapper.like("StatisticDate",  list.get(0).getStatisticDate());
            queryWrapper.last("limit 10");
            queryWrapper.orderByDesc("OnlineRate");
            List<Provincedistribution> provincedistributionList = provincedistributionService.list(queryWrapper);
            return CommonList.success(provincedistributionList);
        }else{
            return CommonList.fail("没有数据");
        }
    }

    @ApiOperation(value="按省分车辆数量TOP10")
    @RequestMapping(value = "provincedistribution/numTop10", method = RequestMethod.GET)
    public CommonList<Provincedistribution> numTop10(){
        //查询有数据的最后一天
        QueryWrapper<Provincedistribution> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<Provincedistribution> list = provincedistributionService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<Provincedistribution> queryWrapper = new QueryWrapper<>();
            //2天前的统计数据
            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            queryWrapper.last("limit 10");
            queryWrapper.orderByDesc("DeviceNum");
            List<Provincedistribution> provincedistributionList = provincedistributionService.list(queryWrapper);
            return CommonList.success(provincedistributionList);
        }else{
            return CommonList.fail("没有数据");
        }
    }
}
